package com.daxiong.pattern.singleton;

import java.util.Random;

/**
 * 模拟多线程访问Person
 *
 * @author sunyanxiong
 * @date 2017/4/23
 */
public class SingletonDemo {


    public static void main(String[] args) {
        CourierThread courier1 = new CourierThread("线程1");
        CourierThread courier2 = new CourierThread("线程2");
        courier1.start();
        courier2.start();
    }

}

class CourierThread extends Thread {
    String[] couriers = {"顺丰快递", "韵达快递", "中通快递", "JD快递"};
    String threadName = null;

    public CourierThread(String threadName) {
        this.threadName = threadName;
    }

    @Override
    public void run() {
        Person person = Person.getPerson();
        Random random = new Random();
        for (int i = 0; i < 5; i++) {
            System.out.println(threadName + ": 我是" + couriers[random.nextInt(couriers.length)] + ",快来去快递。");
            System.out.println(threadName + ": 是的，" + person.getAnswer());
            try {
                this.sleep(1000);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }
}
